using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;
using System.Threading;

namespace ArchSoft
{
    public enum AUTOCAD_VERSION
    {
        NONE = 0,
        UNSUPPORTED,
        CAD2006,
        CAD2007
    }

    public class AutocadHelper
    {
        private static AutocadHelper instance = null;
        //private System.Diagnostics.ProcessStartInfo pri = null;
        //private string StartTime = "";
        //private System.Diagnostics.Process autoCadProcess = null;

        private string templateFileForCreating = string.Empty;
        private string templateFileForPublish = string.Empty;
        private AUTOCAD_VERSION cadVersion = AUTOCAD_VERSION.NONE;
        private string autocadPath = string.Empty;
        private List<string> pageList = null;
        private bool isResetTemplateFile = false;

        private AutocadHelper()
        {
        }
       
        public static AutocadHelper Instance
        {
            get
            {
                if (instance == null)
                    instance = new AutocadHelper();
                return instance;
            }
        }

        public string TemplateFileForCreating
        {
            get
            {
                if ((string.Empty == templateFileForCreating) || (!System.IO.File.Exists(templateFileForCreating)))
                    this.templateFileForCreating = ChooseTemplateFile();
                return templateFileForCreating;
            }

        }

        public string TemplateFileForPublishing
        {
            get
            {
                return templateFileForPublish;
            }
            set
            {
                this.templateFileForPublish = value;
                isResetTemplateFile = true;
            }
        }

        public string ChooseTemplateFile()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = System.IO.Directory.GetCurrentDirectory(); ;
            openFileDialog.CheckFileExists = true;
            openFileDialog.CheckPathExists = true;
            openFileDialog.Title = CADConstants.SELECT_DWT_FILE_TOKEN;
            openFileDialog.Filter = CADConstants.AUTOCAD_DRAWING_TEMPLATE_FILES_FILTER;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
                return openFileDialog.FileName;
            else
                return string.Empty;
        }

        public List<string> PageSetupList
        {
            get
            {
                if ((pageList != null)&&(!this.isResetTemplateFile))
                    return pageList;
                BatchPlot.Publisher publisher = new BatchPlot.Publisher();
                this.isResetTemplateFile = false;
                switch (this.AutocadVersion)
                { 
                    case AUTOCAD_VERSION.CAD2006:
                        return pageList = publisher.GetPageSetupList2006(this.BatchFiles, this.autocadPath, this.templateFileForPublish);
                    case AUTOCAD_VERSION.CAD2007:
                        return pageList = publisher.GetPageSetupList2007(this.BatchFiles, this.autocadPath, this.templateFileForPublish);
                    //Them truong hop hon 2007
                    case AUTOCAD_VERSION.UNSUPPORTED:
                        return pageList = publisher.GetPageSetupList2007(this.BatchFiles, this.autocadPath, this.templateFileForPublish);
                    default:
                        return null;
                }
            }
        }

        public string BatchFiles
        {
            get
            {
                return AppDomain.CurrentDomain.BaseDirectory + CADConstants.BATCH_FILE_DIRECTORY;
            }
        }

        public AUTOCAD_VERSION AutocadVersion
        {
            get
            {
                if (this.cadVersion == AUTOCAD_VERSION.NONE)
                    LoadAutocadVersion();
                return cadVersion;
            }
        }

        public string AutocadPath
        {
            get
            {
                if (this.autocadPath == string.Empty)
                    LoadAutocadVersion();
                return autocadPath;
            }
        }
      

        public string RunAutoCadScript(string pathToSciptFile, bool wait)
        {
            string str = "";
            try
            {
                string par = @"/nologo /b " + "\"" + pathToSciptFile + "\"";
                System.Diagnostics.ProcessStartInfo psi =
                 new System.Diagnostics.ProcessStartInfo(AutocadPath, @"/nologo /b " + "\"" + pathToSciptFile + "\"");
                //new System.Diagnostics.ProcessStartInfo(AutocadPath, @"/nologo /b " + "\"" + pathToSciptFile + "\"");
                psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                psi.CreateNoWindow = true;
                System.Diagnostics.Process autoCadProcess;
                autoCadProcess = System.Diagnostics.Process.Start(psi);
                autoCadProcess.EnableRaisingEvents = false;
                autoCadProcess.WaitForExit();
            }

            catch { }//(Exception ex) { MessageBox.Show(ex.Message + "\nPlease re-check your AutoCAD program.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
            return str;
        }

        private void LoadAutocadVersion()
        {
            try
            {
                RegistryKey cadKey = Registry.LocalMachine.OpenSubKey(CADConstants.CAD_REGISTRY_KEY);

                if (cadKey == null)
                    return;

                foreach (string key in cadKey.GetSubKeyNames())
                {
                    AUTOCAD_VERSION ver = AUTOCAD_VERSION.NONE;

                    switch (key)
                    {
                        case "R16.2":
                            ver = AUTOCAD_VERSION.CAD2006;
                            break;

                        case "R17.0":
                            ver = AUTOCAD_VERSION.CAD2007;
                            break;
                        default:
                            ver = AUTOCAD_VERSION.UNSUPPORTED;
                            break;
                    }

                    if (ver > this.cadVersion)
                    {
                        this.cadVersion = ver;
                        RegistryKey subKey = cadKey.OpenSubKey(key);
                        if (subKey.GetSubKeyNames().Length > 0)
                        {
                            subKey = subKey.OpenSubKey(subKey.GetSubKeyNames()[0]);
                            if (subKey != null)
                                this.autocadPath = (string)subKey.GetValue(CADConstants.CAD_LOCATION_REGISTRY_VALUE_NAME) + System.IO.Path.DirectorySeparatorChar + CADConstants.AUTOCAD_EXECUTE_FILENAME;
                        }
                    }
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message , "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        public static void Public(string pageSetup, string publishFolder,string batchFolder)
        {      
            BatchPlot.Publisher publisher = new BatchPlot.Publisher();
            switch (AutocadHelper.Instance.AutocadVersion)
            {
                case AUTOCAD_VERSION.CAD2006:
                    publisher.Publish2006(AutocadHelper.Instance.AutocadPath, batchFolder, publishFolder, AutocadHelper.Instance.TemplateFileForPublishing, pageSetup);
                    break;
                case AUTOCAD_VERSION.CAD2007:
                    publisher.Publish2007(AutocadHelper.Instance.AutocadPath, batchFolder, publishFolder, AutocadHelper.Instance.TemplateFileForPublishing, pageSetup);
                    break;
                default:
                    publisher.Publish2007(AutocadHelper.Instance.AutocadPath, batchFolder, publishFolder, AutocadHelper.Instance.TemplateFileForPublishing, pageSetup);
                    break;
                //default:
                //    MessageBox.Show("Not supported");
                //    break;
            }
        }
    }
}
